import jsonp from 'common/js/jsonp'
import {commonParams, options} from 'api/config'
import axios from 'axios'
// 原始的jsonp方法没有Promise封装的,没有异步处理
import vJsonp from 'jsonp'

export function getRecommend() {
  // 改接口返回数据为一个对象，具有三个对象属性：slider,songList,audioList
  const url = 'https://c.y.qq.com/musichall/fcgi-bin/fcg_yqqhomepagerecommend.fcg'

  const data = Object.assign({}, commonParams, {
    platform: 'h5',
    uin: 0,
    needNewCode: 1
  })
  return jsonp(url, data, options)
}
export function getDiscList() {
  // 数据返回成功，但是中文乱码了
  const url = 'http://music.qq.com/musicbox/shop/v3/data/hit/hit_newsong.js';
  const opts = {
    param: 'callback',
    name: 'JsonCallback',
    contentType: 'GBK'
  }
  return new Promise((resolve, reject) => {
    vJsonp(url, opts, function (err, res) {
      if (!err) {
        resolve(res)
      } else {
        reject(err)
      }
    })
  })

}
export function getDiscList2() {
  const url = '/api/getDiscList2'
  const data = Object.assign({}, commonParams, {
    platform: 'yqq',
    hostUin: 0,
    sin: 0,
    ein: 29,
    sortId: 5,
    needNewCode: 0,
    categoryId: 10000000,
    rnd: Math.random(),
    format: 'json'
  })
  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}
export function getSongList(disstid) {
  const url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg'
  const data = Object.assign({}, commonParams, {
    disstid,
    type: 1,
    json: 1,
    utf8: 1,
    onlysong: 0,
    platform: 'yqq',
    hostUin: 0,
    needNewCode: 0
  })

  return jsonp(url, data, options)
}
